package cn.lanqiao.book.service.impl;


import cn.lanqiao.book.dao.IUserDao;
import cn.lanqiao.book.dao.impl.UserDaoImpl;
import cn.lanqiao.book.pojo.User;
import cn.lanqiao.book.service.IUserService;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;


public class UserServiceImpl implements IUserService {
    private static UserServiceImpl userService;
    private UserServiceImpl(){}
    public static UserServiceImpl getInstance(){
        if (userService==null){
            userService=new UserServiceImpl();
        }
        return userService;
    }
    private IUserDao IUserDao =new UserDaoImpl();
    //登录验证
    @Override
    public  Boolean checkPassword(String username, String password) {

        if(password.equals(IUserDao.queryPassword(username)))
        {return true;}
        else
        { return false;}
    }
    //注册账号
    @Override
    public  Boolean register(User user){
        if(IUserDao.addUser(user)!=1)
        {return false;}
        else
        { return true;}
    }
    //验证用户名是否已存在
    @Override
    public  Boolean existUsername(String username){
        if(IUserDao.queryPassword(username)==null){
            return false;
        }
        else
        {return true;}
    }
   //检查username与email是否相匹配
    @Override
    public Boolean checkEmail(String username, String email) {
        if(email.equals(IUserDao.queryEmail(username))){
            return true;
        }
        else {return false;}
    }
    //重置密码，将用户名为 username的用户密码重置为 NewPassword
    @Override
    public Boolean resetPassword(String username, String NewPassword) {
        return IUserDao.updatePassword(username,NewPassword);
    }
    //发送验证码
    @Override
    public void sendVerificationCode(String email,int number) {
        //收件人
        String to =email;
        System.out.println("收件人为"+to);
        //这里输入一个官方邮箱
        String from="pyh12138whm@163.com";
        //该邮箱的第三方客户端授权密码
        String password="HUBZUZOXRTKOJRFU";
        // 邮件的主题
        String subject="验证码";
        //邮件的内容
        String content="本次的验证密码为"+number;
        System.out.println(content);
        try {
            //创建参数配置
            Properties properties=getProperties();
            //根据配置创建会话对象，用于和邮件服务器交互
            Session session=Session.getInstance(properties);
            session.setDebug(true);
            //创建一封邮件
            MimeMessage mimeMessage=createSimpleMimeMessage(session,from,to,subject,content);
            //根据session获取邮件传输对象
            Transport transport=session.getTransport();
            //使用邮箱账号和密码连接邮件服务器，这里认证的邮箱必须与message中的发件人邮箱一致
            transport.connect(from,password);
            // 发送邮件
            transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
            //关闭连接
            transport.close();
            System.out.println("发送成功");
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }
    public MimeMessage createSimpleMimeMessage(Session session , String sendMail, String receiveMail, String subject, String content) throws MessagingException {

        MimeMessage message = new MimeMessage(session);
        //设置发件人
        message.setFrom(new InternetAddress(sendMail));
        //设置收件人
        message.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(receiveMail));
        //设置邮件主题
        message.setSubject(subject,"UTF-8");
        //设置邮件内容
        message.setContent(content,"text/html;charset=UTF-8");
        //设置发件时间
        message.setSentDate(new Date());
        //保存设置
        message.saveChanges();

        return message;

    }
    public Properties getProperties(){
        Properties properties=new Properties();
        properties.setProperty("mail.transport.protocol","smtp");
        properties.setProperty("mail.smtp.host","smtp.163.com");
        properties.setProperty("mail.smtp.auth","true");
        return properties;
    }
}
